<?php
class parseFile{
	private $dir;
	private $file;
	private $fieldArr;

	public function __construct($file){
		$this->dir = dirname($file);
		$this->file=$file;
		$this->fieldArr=array();
	}

	public function formatData($dataRow){
		$this->formatField($dataRow);
		$lineData='';
		$isfir=true;
		foreach($dataRow as $index => $data){
			$data=str_replace("\t",'',$data);
			if($isfir){
				$lineData.=$data;
				$isfir=false;
			}else{
				$lineData.="\t,\t";
				$lineData.=$data;
			}
		}

		return($lineData);
	}

	private function formatField($dataRow){
		$index=0;
		$field=array();
		foreach($dataRow as $key => $row){
			$field[$key]=$index;
			$index++;
		}
		
		$fieldStr = var_export($field,true);
		$fieldStr ='<?php'."\n".'$field='.$fieldStr.";\n";

		$this->touchFile($this->file.".php");
		$this->writeToFile($fieldStr , $this->file.".php");
	}

	public function writeToFile($dataLine , $file=false){
		$file = $file ? $file : $this->file;
		$fp=fopen($file,'a');

		$dataLine.="\n";
		fwrite($fp,$dataLine);
		fclose($fp);
	}

	public function readDataLine($line , $file=false){
		$file = $file ? $file : $this->file;
		$fp=fopen($file,'r');

		$curLine=0;
		$data=false;
		while(!feof($fp)){
			$curLine++;
			if($line===$curLine){
				$data=fgets($fp);
				break;
			}else{
				fgets($fp);	
			}
		}

		fclose($fp);

		return ($data);
	}

	public function readData(&$data , $file=false){
		$file = $file ? $file : $this->file;
		$data='';
		$fp=fopen($file,'r');

		$curLine=0;
		while(!feof($fp)){
			$data.=fgets($fp);
			$curLine++;
		}

		fclose($fp);

		return ($curLine);
	}

	public function searchData($key , $lineArr=false , $field=false , $file=false){
		$dataLine = $this->searchDataCommon($key,$lineArr,$field,$file);
		return ($dataLine);
	}

	public function pregData($key , $lineArr=false , $field=false , $file=false){
		$dataLine = $this->searchDataCommon($key,$lineArr,$field,$file,'preg_match');
		return ($dataLine);
	}

	public function equeData($key , $lineArr=false , $field=false , $file=false){
		$dataLine = $this->searchDataCommon($key,$lineArr,$field,$file,'==');
		return ($dataLine);
	}

	public function searchDataCommon($key , $lineArr , $field , $file , $fn='stristr'){
		$file = $file ? $file : $this->file;
		$data='';
		$fp=fopen($file,'r');

		$curLine=0;
		$dataLine=array();
		$lineList=array();
		while(!feof($fp)){
			$curLine++;
			$data=fgets($fp);
			if($lineArr){
				if(in_array($curLine,$lineArr)){
					$fieldData=$data;
					if($field){						
						$fieldData = $this->getFieldData($data,$field);
						if(empty($data)){
							continue;
						}
					}
					
					if($fn=='stristr'){
						if(stristr($fieldData,$key)!==false){
							$dataLine[]=$curLine;
						}
					}elseif($fn=='preg_match'){
						if(preg_match($key , $fieldData)){
							$dataLine[]=$curLine;
						}
					}else{
						if($fieldData==$key){
							$dataLine[]=$curLine;
						}
					}
				}
			}else{
				if($fn=='stristr'){
					if(stristr($data,$key)!==false){
						$dataLine[]=$curLine;
					}
				}elseif($fn=='preg_match'){
					if(preg_match($key , $data)){
						$dataLine[]=$curLine;
					}
				}else{
					if($key == $data){
						$dataLine[]=$curLine;
					}
				}
			}
		}

		fclose($fp);

		return ($dataLine);
	}

	public function getFieldData($data , $key){
		if(count($this->fieldArr) <= 0){
			require($this->file.".php");
			$this->fieldArr=$field;
		}

		$dataArr=explode("\t,\t",$data);
		$tmpdata=false;
		if(isset($this->fieldArr[$key])){
			$index = $this->fieldArr[$key];
			if(isset($dataArr[$index])){
				$tmpdata = trim($dataArr[$index]);
			}
		}

		return ($tmpdata);
	}

	public function touchFile($file=false){
		$tmpdir=dirname($file);
		if(!realpath($tmpdir)){
			mkdir($tmpdir,0777,true);
		}
		$file = $file ? $file : $this->file;
		$fp=fopen($file,'w');
		fclose($fp);
	}

	public function delFile($file=false){
		$file = $file ? $file : $this->file;
		unlink($file);
	}


	public function getLines($file=false){
		$file = $file ? $file : $this->file;
		$fp=fopen($file,'r');

		$curLine=0;
		while(!feof($fp)){
			$curLine++;
			fgets($fp);
		}

		fclose($fp);

		return ($curLine);
	}
}